package pers.zw.study.mysql.utils;

import java.sql.*;

public class JdbcPoolUtil {

    private static final JdbcPool pool = new JdbcPool();

    public static Connection getConnection() throws SQLException {
        return pool.getConnection();
    }

    public static void release(Connection connection, Statement statement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                //关闭存储查询结果的ResultSet对象
                resultSet.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            resultSet = null;
        }
        if(statement != null) {
            try{
                //关闭负责执行SQL命令的Statement对象
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(connection != null) {
            try {
                //关闭Connection数据库连接对象
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JdbcPoolUtil.getConnection();
            String sql1 = "UPDATE student SET s_class = 99999 WHERE s_no = 109";
            preparedStatement = connection.prepareStatement(sql1);
            preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcPoolUtil.release(connection, preparedStatement, null);
        }
    }
}
